package me.firstapp.weixin.controller.account;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import me.firstapp.common.exception.ServiceException;
import me.firstapp.common.exception.StatusHouse;
import me.firstapp.common.json.ListObject;
import me.firstapp.common.utils.ResponseUtils;
import me.firstapp.module.weixin.jinbi.JinBiAccount;
import me.firstapp.module.weixin.jinbi.JinBiAccountTrace;
import me.firstapp.module.weixin.member.Member;
import me.firstapp.module.weixin.weibi.WeiBiAccount;
import me.firstapp.module.weixin.weibi.WeiBiAccountTrace;
import me.firstapp.service.weixin.account.JinBiAccountService;
import me.firstapp.service.weixin.account.JinBiAccountTraceService;
import me.firstapp.service.weixin.account.WeiBiAccountService;
import me.firstapp.service.weixin.account.WeiBiAccountTraceService;
import me.firstapp.service.weixin.member.MemberService;
import me.firstapp.weixin.controller.base.BaseController;

@Controller
public class AccountController extends BaseController {

	@Autowired
	private WeiBiAccountTraceService weiBiAccountTraceService;

	@Autowired
	private WeiBiAccountService weiBiAccountService;

	@Autowired
	private MemberService memberService;

	@Autowired
	private JinBiAccountService jinBiAccountService;

	@Autowired
	private JinBiAccountTraceService jinBiAccountTraceService;

	@RequestMapping("/page/weiBiRecordPage.htm")
	public ModelAndView showWeiBiRecordPage(HttpServletRequest request, HttpServletResponse response,
			ModelMap modelMap) {
		String openId = super.getCurrentLoginOpenId(request, response);
		Member member = memberService.findMemberByOpenId(openId);
		if (null == member) {
			throw new ServiceException(StatusHouse.STATUS_PAGE_ERROR_NOT_CONCERN_WEIXIN);
		}
		WeiBiAccount weiBiAccount = weiBiAccountService.findWeiBiAccountByCardNoAndOpenId(member.getCardNo(), openId);
		modelMap.put("balance", weiBiAccount.getBalance());
		return new ModelAndView("account/weibiRecord", modelMap);
	}

	@RequestMapping("/page/getWeiBiAccountTraceData.do")
	public void getWeiBiAccountTraceData(@RequestParam(defaultValue = "1") int pageIndex,
			@RequestParam(defaultValue = "20") int pageSize, HttpServletRequest request, HttpServletResponse response) {
		ListObject<JsonWeiBiAccountTrace> result = new ListObject<JsonWeiBiAccountTrace>();
		try {
			String openId = super.getCurrentLoginOpenId(request, response);
			Member member = memberService.findMemberByOpenId(openId);
			if (null == member) {
				throw new ServiceException(StatusHouse.STATUS_PAGE_ERROR_NOT_CONCERN_WEIXIN);
			}
			WeiBiAccount weiBiAccount = weiBiAccountService.findWeiBiAccountByCardNoAndOpenId(member.getCardNo(),
					openId);
			List<JsonWeiBiAccountTrace> jsonWeiBiAccountTraces = new ArrayList<JsonWeiBiAccountTrace>();
			List<WeiBiAccountTrace> traces = weiBiAccountTraceService
					.findWeiBiAccountTracePageByOpenId(weiBiAccount.getId(), pageIndex, pageSize);
			for (WeiBiAccountTrace weiBiAccountTrace : traces) {
				JsonWeiBiAccountTrace jsonWeiBiAccountTrace = new JsonWeiBiAccountTrace(weiBiAccountTrace);
				jsonWeiBiAccountTraces.add(jsonWeiBiAccountTrace);
			}
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			result.setItems(jsonWeiBiAccountTraces);
		} catch (ServiceException e) {
			e.printStackTrace();
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping("/page/jinBiRecordPage.htm")
	public ModelAndView showJinBiRecordPage(HttpServletRequest request, HttpServletResponse response,
			ModelMap modelMap) {
		String openId = super.getCurrentLoginOpenId(request, response);
		Member member = memberService.findMemberByOpenId(openId);
		if (null == member) {
			throw new ServiceException(StatusHouse.STATUS_PAGE_ERROR_NOT_CONCERN_WEIXIN);
		}
		JinBiAccount jinBiAccount = jinBiAccountService.findJinBiAccountByCardNoAndOpenId(member.getCardNo(), openId);
		modelMap.put("balance", jinBiAccount.getBalance());
		return new ModelAndView("account/jinbiRecord", modelMap);
	}

	@RequestMapping("/page/getJinBiAccountTraceData.do")
	public void getJinBiAccountTraceData(@RequestParam(defaultValue = "1") int pageIndex,
			@RequestParam(defaultValue = "20") int pageSize, HttpServletRequest request, HttpServletResponse response) {
		ListObject<JsonJinBiAccountTrace> result = new ListObject<JsonJinBiAccountTrace>();
		try {
			String openId = super.getCurrentLoginOpenId(request, response);
			Member member = memberService.findMemberByOpenId(openId);
			if (null == member) {
				throw new ServiceException(StatusHouse.STATUS_PAGE_ERROR_NOT_CONCERN_WEIXIN);
			}
			JinBiAccount jinBiAccount = jinBiAccountService.findJinBiAccountByCardNoAndOpenId(member.getCardNo(),
					openId);
			List<JsonJinBiAccountTrace> jsonJinBiAccountTraces = new ArrayList<JsonJinBiAccountTrace>();
			List<JinBiAccountTrace> traces = jinBiAccountTraceService
					.findJinBiAccountTracePageByOpenId(jinBiAccount.getId(), pageIndex, pageSize);
			for (JinBiAccountTrace jinBiAccountTrace : traces) {
				JsonJinBiAccountTrace jsonWeiBiAccountTrace = new JsonJinBiAccountTrace(jinBiAccountTrace);
				jsonJinBiAccountTraces.add(jsonWeiBiAccountTrace);
			}
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			result.setItems(jsonJinBiAccountTraces);
		} catch (ServiceException e) {
			e.printStackTrace();
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

}
